<template>
  <div class="zy-content">
    <div class="searchBox search-box-layout">
      <Row :gutter="24">
        <Col span="4">
          <div>
            <p class="label">物料编号</p>
            <div class="ipt">
              <Input v-model="params.materialCode" clearable :placeholder="`${$t('common.inputInfo')}`"
                     class="inputBorder"></Input>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">物料名称</p>
            <div class="ipt">
              <Input v-model="params.materialName" clearable :placeholder="`${$t('common.inputInfo')}`"
                     class="inputBorder"></Input>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">起始位</p>
            <div class="ipt">
              <InputNumber v-model="params.startIndex" :min="0" :placeholder="`${$t('common.inputInfo')}`"
                           style="width: 100%" class="inputBorder"></InputNumber>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">结束位</p>
            <div class="ipt">
              <InputNumber v-model="params.endIndex" :min="0" :placeholder="`${$t('common.inputInfo')}`"
                           style="width: 100%" class="inputBorder"></InputNumber>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">校验值</p>
            <div class="ipt">
              <Input v-model="params.comparisonValues" clearable :placeholder="`${$t('common.inputInfo')}`"
                     class="inputBorder"></Input>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label hide-label">--</p>
            <div class="ipt">
              <Button class="search-btn" type="primary" @click="changeNum()">{{ $t('common.Query') }}</Button>
              <Button class="reset-btn" @click="reset()">{{ $t('common.reset') }}</Button>
            </div>
          </div>
        </Col>
      </Row>
    </div>
    <div class="list-layout mt16">
      <div class="btns clear">
        <span class="title">{{ $t('common.listInformation') }}</span>
        <Button @click="addHandle">
          {{ $t('common.New') }}
        </Button>
        <Button @click="importExcelShow">导入</Button>
        <Button @click="exportTemplate">下载模板</Button>
      </div>
      <div class="tableContent">
        <Table ellipsis ref="selection" class="resizable-table" border @on-column-width-resize="onColumnWidthResize"
               :columns="columnsList" :data="list" height="520">
          <template slot-scope="{ row }" slot="action">
            <span class="operationBtn" @click="editRow(row)"><i class="iconfont zy-common-bianji"></i>{{
                $t('common.Edit')
              }}</span>
            <span class="operationBtn" @click="delRow(row)"><i class="iconfont zy-common-shanchu"></i>{{
                $t('common.Delete')
              }}</span>
          </template>
        </Table>
      </div>
      <div class="pageWrap mt16">
        <pageLeftTitle :total="total"/>
        <Page :total="total" :page-size="params.pageSize" :current="params.pageNo" @on-change="changePageNum"
              @on-page-size-change="changePageSize" show-sizer/>
      </div>
    </div>
    <Modal :mask-closable="false" v-model="deleteShow" :title="$t('common.Delete')" class="modal-title-layout">
      <div class="icon red">
        <i class="iconfont zy-common-shanchu"></i>
      </div>
      <p class="title">
        <span>{{ $t('common.sureDelete') }}</span>
      </p>
      <div slot="footer">
        <Button type="default" @click="deleteShow = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="deleteSure">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <Modal :mask-closable="false" v-model="createStatus" :title="form.id ? $t('common.Edit') : $t('common.New')"
           class="modal-content-layout">
      <Form v-if="createStatus" class="formList" :model="form" label-position="top" ref="form" :rules="rules">
        <FormItem label="物料编号" required prop="materialCode">
          <!--          <Input type="text" v-model="form.materialCode" :placeholder="`${$t('common.inputInfo')}`"-->
          <!--                 class="inputBorder"></Input>-->
          <Select
              v-model="form.materialCode"
              filterable
              clearable
              :remote-method="handleRemote"
              style="width:400px"
              placeholder="请输入物料编号"
          >
            <Option
                v-for="item in numberList"
                :value="item.materialCode"
                :key="item.materialCode"
            >{{ item.materialCode + ' - ' + item.materialName }}
            </Option>
          </Select>
        </FormItem>
        <FormItem label="起始位" required prop="startIndex">
          <InputNumber v-model="form.startIndex" :min="0" :placeholder="`${$t('common.inputInfo')}`"
                       class="inputBorder" style="width: 400px"></InputNumber>
        </FormItem>
        <FormItem label="结束位" required prop="endIndex">
          <InputNumber v-model="form.endIndex" :min="0" :placeholder="`${$t('common.inputInfo')}`"
                       class="inputBorder" style="width: 400px"></InputNumber>
        </FormItem>
        <FormItem label="校验值" required prop="comparisonValues">
          <Input type="text" v-model="form.comparisonValues" :placeholder="`${$t('common.inputInfo')}`"
                 class="inputBorder" style="width: 400px"></Input>
        </FormItem>
      </Form>
      <div slot="footer" v-if="createStatus">
        <Button type="default" @click="createStatus = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="addHandleSubmit('form')">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <importExcelModal
        :importExcelStateF="importExcelState"
        v-if="importExcelState"
        url="/dms/basicMaterialValue/importExcel"
        @importExcelChange="importExcelChange"
    ></importExcelModal>

  </div>
</template>

<script>
import toggleTableOperation from "@/mixins/toggleTableOperation";
import importExcelModal from '@/components/importExcelNew.vue';
import {basicMaterialValueExcelTemplate} from "@/api/moldManagement/moldLedger";
import fileDownload from 'js-file-download';

export default {
  name: "basicMaterialValue",
  components: {importExcelModal},
  mixins: [toggleTableOperation],
  data() {
    return {
      importExcelState: false /*批量导入*/,
      numberList: [],
      params: {
        pageSize: 20,
        pageNo: 1,
        materialCode: '',
        startIndex: null,
        endIndex: null,
        comparisonValues: ''
      },
      columns: [
        {
          title: '物料编号',
          key: 'materialCode',
          align: 'center',
          resizable: true
        },
        {
          title: '物料名称',
          key: 'materialName',
          align: 'center',
          resizable: true
        },
        {
          title: '起始位',
          key: 'startIndex',
          align: 'center',
          resizable: true
        },
        {
          title: '结束位',
          key: 'endIndex',
          align: 'center',
          resizable: true
        },
        {
          title: '校验值',
          key: 'comparisonValues',
          align: 'center',
          resizable: true
        },
        {
          title: `${this.$t('common.operation')}`,
          slot: "action",
          width: 200,
          align: "center",
          fixed: "right"
        }
      ],
      list: [],
      total: 0,
      deleteShow: false,
      currentItem: {},
      createStatus: false,
      form: {
        status: 1
      },
      rules: {
        materialCode: [
          {required: true, message: this.$t('common.inputInfo'), trigger: "blur"},
        ],
        startIndex: [
          {required: true, type: 'number', message: this.$t('common.inputInfo'), trigger: "blur"},
        ],
        endIndex: [
          {required: true, type: 'number', message: this.$t('common.inputInfo'), trigger: "blur"},
        ],
        comparisonValues: [
          {required: true, message: this.$t('common.inputInfo'), trigger: "blur"},
        ],
      }
    }
  },
  created() {
    this.getTableColumnsWidth(this.columns)
  },
  mounted() {
    this.getList()
    this.handleRemote(1)
  },
  methods: {
    exportTemplate() {
      const filName = '料号取值导入模板';
      basicMaterialValueExcelTemplate().then((res) => {
        fileDownload(res, filName + '.xlsx');
      });
    },
    importExcelShow() {
      this.importExcelState = true;
    },
    importExcelChange(val) {
      this.importExcelState = false;
      if (val === 'success') {
        this.changeNum();
      }
    },
    handleRemote(query) {
      if (query !== '') {
        this.axios({
          method: 'POST',
          url: '/dms/material/page',
          data: {
            materialCode: query,
            pageNum: 1,
            pageSize: 20
          }
        }).then(res => {
          if (res.code === 200) {
            this.numberList = res.data.records
          } else {
            this.$Message.warning(({content: res.msg, duration: 6, closable: true}))
          }
        })
      } else {
        this.numberList = [];
      }
    },
    getList() {
      this.axios({
        method: 'get',
        url: '/dms/basicMaterialValue/listPage',
        params: {
          pageNum: this.params.pageNo,
          pageSize: this.params.pageSize,
          materialCode: this.params.materialCode,
          startIndex: this.params.startIndex,
          endIndex: this.params.endIndex,
          comparisonValues: this.params.comparisonValues
        }
      }).then(res => {
        if (res.code === 200) {
          this.list = res.data.list;
          this.total = res.data.total;
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      })
    },
    reset() {
      this.params = {
        pageSize: 20,
        pageNo: 1,
        materialCode: '',
        startIndex: null,
        endIndex: null,
        comparisonValues: ''
      };
      this.changeNum();
    },
    changeNum() {
      this.params.pageNo = 1
      this.params.pageSize = 20
      this.getList()
    },
    addHandle() {
      this.form = {
        status: 1,
        startIndex: 0,  // 默认值
        endIndex: 0,    // 默认值
      };
      this.createStatus = true
    },
    addHandleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const url = this.form.id ? '/dms/basicMaterialValue/update' : '/dms/basicMaterialValue/add';
          const method = this.form.id ? 'put' : 'post';

          this.axios({
            method: method,
            url: url,
            data: {
              ...this.form
            }
          }).then(res => {
            if (res.code === 200) {
              this.$Message.success(res.msg);
              this.createStatus = false;
              this.getList();
            } else {
              this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
            }
          })
        }
      })
    },
    editRow(row) {
      this.form = {...row};
      if (row.materialCode) {
        this.handleRemote(row.materialCode)
      }
      this.createStatus = true;
    },
    delRow(row) {
      this.currentItem = row;
      this.deleteShow = true;
    },
    deleteSure() {
      this.axios({
        method: 'delete',
        url: `/dms/basicMaterialValue/delete?id=${this.currentItem.id}`
      }).then(res => {
        if (res.code === 200) {
          this.$Message.success(res.msg);
          this.deleteShow = false;
          this.getList();
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      })
    },
    changePageNum(val) {
      this.params.pageNo = val
      this.getList()
    },
    changePageSize(val) {
      this.params.pageSize = val
      this.getList()
    }
  }
}
</script>

<style scoped>
</style>